import os
import sys
import warnings

import numpy as np
import pandas as pd

sys.path.append('..')
warnings.filterwarnings('ignore')

from tools.setting import DATA_DIR

ANALYSIS_DATA_DIR = os.path.join(DATA_DIR, 'analysis')
os.makedirs(ANALYSIS_DATA_DIR, exist_ok=True)
CEX_ANALYSIS_DATA_DIR = os.path.join(ANALYSIS_DATA_DIR, 'cex')
os.makedirs(CEX_ANALYSIS_DATA_DIR, exist_ok=True)


def cex_analysis():
    file_path = os.path.join(DATA_DIR, f'amount_data')

    #### 统计交易所平台币相关信息 ###
    df_btc = pd.read_csv(os.path.join(DATA_DIR, f'ohlcvm_data\\daily\\bitcoin.csv'), index_col='end_date')
    df_btc.sort_index(inplace=True)
    btc_price = df_btc.loc['2017-07-26':, 'close']
    btc_price.name = 'bitcoin_price'
    btc_log_price = np.log10(df_btc.loc['2017-07-26':, 'close'])
    btc_log_price.name = 'bitcoin_log_price'

    # 各交易所的现货交易额汇总
    df_all_cex_spot_amount = pd.read_csv(os.path.join(file_path, 'cex_spot_amount_coingecko.csv'), index_col='date')
    df_all_cex_spot_amount.sort_index(inplace=True)

    # 各交易所的衍生品交易额汇总
    df_all_cex_deri_amount = pd.read_csv(os.path.join(file_path, 'cex_deri_amount_coingecko.csv'), index_col='date')
    df_all_cex_deri_amount.sort_index(inplace=True)

    # 各交易所现货,衍生品交易产生的手续费收入汇总
    df_cex_spot_fee = df_all_cex_spot_amount * [0.001, 0.005, 0.0008, 0.002, 0.002, 0.001, 0.004, 0.001, 0.001]
    df_cex_deri_fee = df_all_cex_deri_amount * [0.00018, 0.0002, 0.0002, 0.0002, 0.0005, 0.0002, 0.00015, 0.0002, 0.0002, 0.0002]

    # 手续费收入按照季度汇总
    df_cex_spot_fee.index = pd.to_datetime(df_cex_spot_fee.index)
    df_cex_spot_fee_season = df_cex_spot_fee.resample('Q').sum()
    df_cex_deri_fee.index = pd.to_datetime(df_cex_deri_fee.index)
    df_cex_deri_fee_season = df_cex_deri_fee.resample('Q').sum()
    df_cex_deri_fee_season['okex'] = 0  # okex 回购中不包含 衍生品交易带来的收入
    df_cex_deri_fee_season['coinbase'] = 0  # coinbase 没有衍生品业务
    cex_list = list(set(df_cex_spot_fee.columns) | set(df_cex_deri_fee.columns))
    for cex in cex_list:
        if cex not in df_cex_spot_fee_season.columns:
            df_cex_spot_fee_season[cex] = 0
        if cex not in df_cex_deri_fee_season.columns:
            df_cex_deri_fee_season[cex] = 0
    df_cex_fee_season = df_cex_spot_fee_season + df_cex_deri_fee_season
    file_name = os.path.join(CEX_ANALYSIS_DATA_DIR, f'cex_season_fee.csv')
    df_cex_fee_season.to_csv(file_name)

    # 各交易所现货交易额市场占比
    spot_amount_ratio_list = []
    for exchange in df_all_cex_spot_amount.columns:
        volume_ratio = df_all_cex_spot_amount[exchange] / df_all_cex_spot_amount.sum(axis=1)
        volume_ratio = volume_ratio.rolling(7).mean()
        volume_ratio.name = exchange + '_ratio'
        spot_amount_ratio_list.append(volume_ratio)
    total_volume = df_all_cex_spot_amount.sum(axis=1)
    total_volume.name = 'all_cex_spot_amount'
    spot_amount_ratio_list.append(total_volume)
    df_spot_amount_ratio = pd.concat(spot_amount_ratio_list, axis=1)

    # 各交易所衍生品交易额市场占比
    deri_amount_ratio_list = []
    for exchange in df_all_cex_deri_amount.columns:
        volume_ratio = df_all_cex_deri_amount[exchange] / df_all_cex_deri_amount.sum(axis=1)
        volume_ratio = volume_ratio.rolling(7).mean()
        volume_ratio.name = exchange + '_ratio'
        deri_amount_ratio_list.append(volume_ratio)
    total_volume = df_all_cex_deri_amount.sum(axis=1)
    total_volume.name = 'all_cex_deri_amount'
    deri_amount_ratio_list.append(total_volume)
    df_deri_amount_ratio = pd.concat(deri_amount_ratio_list, axis=1)

    # 各交易所现货交易带来的利润市场占比
    spot_fee_ratio_list = []
    for exchange in df_cex_spot_fee.columns:
        spot_fee_ratio = df_cex_spot_fee[exchange] / df_cex_spot_fee.sum(axis=1)
        spot_fee_ratio = spot_fee_ratio.rolling(7).mean()
        spot_fee_ratio.name = exchange + '_ratio'
        spot_fee_ratio_list.append(spot_fee_ratio)
    total_fee = df_cex_spot_fee.sum(axis=1)
    total_fee.name = 'all_cex_spot_fee'
    spot_fee_ratio_list.append(total_fee)
    df_spot_fee_ratio = pd.concat(spot_fee_ratio_list, axis=1)

    # 各交易所衍生品交易带来的利润市场占比
    deri_fee_ratio_list = []
    for exchange in df_cex_deri_fee.columns:
        deri_fee_ratio = df_cex_deri_fee[exchange] / df_cex_deri_fee.sum(axis=1)
        deri_fee_ratio = deri_fee_ratio.rolling(7).mean()
        deri_fee_ratio.name = exchange + '_ratio'
        deri_fee_ratio_list.append(deri_fee_ratio)
    total_fee = df_cex_deri_fee.sum(axis=1)
    total_fee.name = 'all_cex_deri_fee'
    deri_fee_ratio_list.append(total_fee)
    df_deri_fee_ratio = pd.concat(deri_fee_ratio_list, axis=1)

    all_exchange_spot_amount = pd.concat([df_all_cex_spot_amount, df_spot_amount_ratio], axis=1)
    all_exchange_spot_amount = pd.concat([all_exchange_spot_amount, btc_log_price.reindex(all_exchange_spot_amount.index)], axis=1)
    all_exchange_spot_amount['binance_spot_volume_ma7'] = all_exchange_spot_amount['binance'].rolling(7).mean()
    file_name = os.path.join(CEX_ANALYSIS_DATA_DIR, f'all_cex_spot_amount')
    all_exchange_spot_amount.to_csv(f'{file_name}.csv')

    all_exchange_spot_fee = pd.concat([df_cex_spot_fee, df_spot_fee_ratio], axis=1)
    all_exchange_spot_fee = pd.concat([all_exchange_spot_fee, btc_log_price.reindex(all_exchange_spot_fee.index)], axis=1)
    file_name = os.path.join(CEX_ANALYSIS_DATA_DIR, f'all_cex_spot_fee')
    all_exchange_spot_fee.to_csv(f'{file_name}.csv')

    all_deri_exchange_amount = pd.concat([df_all_cex_deri_amount, df_deri_amount_ratio], axis=1)
    all_deri_exchange_amount = pd.concat([all_deri_exchange_amount, btc_log_price.reindex(all_deri_exchange_amount.index)], axis=1)
    all_deri_exchange_amount['binance_deri_volume_ma7'] = all_deri_exchange_amount['binance'].rolling(7).mean()
    file_name = os.path.join(CEX_ANALYSIS_DATA_DIR, f'all_cex_deri_amount')
    all_deri_exchange_amount.to_csv(f'{file_name}.csv')

    all_exchange_deri_fee = pd.concat([df_cex_deri_fee, df_deri_fee_ratio], axis=1)
    all_exchange_deri_fee = pd.concat([all_exchange_deri_fee, btc_log_price.reindex(all_exchange_deri_fee.index)], axis=1)
    file_name = os.path.join(CEX_ANALYSIS_DATA_DIR, f'all_cex_deri_fee')
    all_exchange_deri_fee.to_csv(f'{file_name}.csv')


if __name__ == '__main__':
    cex_analysis()
